package com.see.domain.dao;

import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;

import com.see.domain.model.StaticData;

public final class DataNucleusUtil {

	public List<StaticData> getFullListOfRecords() {
		
		PersistenceManager persistenceManager = PMF.get().getPersistenceManager();
		javax.jdo.Transaction tx=persistenceManager.currentTransaction();
		try {
			tx.begin();
			Query query = persistenceManager.newQuery(StaticData.class);
			List<StaticData> staticDatas = (List<StaticData>) persistenceManager.newQuery(query).execute();
			tx.commit();
			return staticDatas;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			 if (tx.isActive())
			    {
			        tx.rollback();
			    }
			persistenceManager.close();
		}
		return null;
	}

	public List<StaticData> getRecordBySymbol(String symbol) {
		PersistenceManager persistenceManager = PMF.get().getPersistenceManager();
		try {
			Query query = persistenceManager.newQuery(StaticData.class);
			query.setFilter("symbol == symbolParam");
			query.declareParameters("String symbolParam");

			List<StaticData> staticDatas = (List<StaticData>) persistenceManager.newQuery(query).execute(symbol);

			return staticDatas;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			persistenceManager.close();
		}

		return null;
	}

	public List<StaticData> getRecordBySector(String sector) {
		PersistenceManager persistenceManager = PMF.get().getPersistenceManager();
		try {
			Query query = persistenceManager.newQuery(StaticData.class);
			query.setFilter("sector == sectorParam");
			query.declareParameters("String sectorParam");
			List<StaticData> staticDatas = (List<StaticData>) PMF.get()
					.getPersistenceManager().newQuery(query).execute(sector);

			return staticDatas;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			persistenceManager.close();
		}

		return null;
	}

	public List<StaticData> getRecordByIndustry(String industry) {
		PersistenceManager persistenceManager = PMF.get().getPersistenceManager();
		try {
			Query query = persistenceManager.newQuery(StaticData.class);
			query.setFilter("industry == industryParam");
			query.declareParameters("String industryParam");
			List<StaticData> staticDatas = (List<StaticData>) PMF.get()
					.getPersistenceManager().newQuery(query).execute(industry);

			return staticDatas;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			persistenceManager.close();
		}
		return null;
	}

	public void updateSectorAndIndustryBySymbol(String symbol, String sector,
			String industry) {
		PersistenceManager persistenceManager = PMF.get().getPersistenceManager();
		Transaction trans  = persistenceManager.currentTransaction();
		try {
			trans.begin();
			Query query = persistenceManager.newQuery(StaticData.class);
			query.setFilter("symbol == symbolParam");
			query.declareParameters("String symbolParam");
			List<StaticData> list = (List<StaticData>) persistenceManager.newQuery(query).execute(symbol);
			StaticData staticDatas = list.get(0);
			persistenceManager.getExtent(staticDatas.getClass(),true);
			staticDatas.setSector(sector);
			staticDatas.setIndustry(industry);
			
			trans.commit();
			
		} finally {
			
			if(persistenceManager.currentTransaction().isActive())
			{
				persistenceManager.currentTransaction().rollback();
			}
			persistenceManager.close();
		}
		
		
		
	}

}
